package com.etcfg.etlaunch.ui;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.HyperlinkEvent.EventType;

import com.etcfg.etlaunch.ApplicationSetting;
import com.etcfg.etlaunch.UpdateChecker;

public class AboutDialog extends JDialog {

	public AboutDialog() {
		setModal(true);
		setAlwaysOnTop(true);

		setLocationByPlatform(true);
		JEditorPane editorPane = new JEditorPane();
		editorPane.setContentType("text/html");
		editorPane
				.setText("<html>"
						+ "<p><font>etcfg.Launch "
						+ ApplicationSetting.VERSION
						+ "</font></p>"
						+ "<p>Created by Alexey \"jusio\" Bykov </p>"
						+ "<p><a href='http://code.google.com/p/etcfglaunch/'>Site</a></p>"
						+ "<p>IRC: #etcfg @ Quakenet</p>"
						+ "<p>Email: <a href='mailto:jusioa@etcfg.com'>jusioa@etcfg.com</a></p>"
						+ "Special thanks to Fubor for help with testing"
						+ "</html>");
		editorPane.addHyperlinkListener(new HyperlinkListener() {

			@Override
			public void hyperlinkUpdate(HyperlinkEvent e) {
				if (EventType.ACTIVATED == e.getEventType()) {
					URL url = e.getURL();
					try {
						Desktop.getDesktop().browse(url.toURI());
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (URISyntaxException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}

			}
		});
		editorPane.setEditable(false);

		JPanel buttonPanel = new JPanel();

		JButton closeButton = new JButton("Close");
		JButton checkUpdatesButton = new JButton("Check for updates");

		closeButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		checkUpdatesButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				UpdateChecker checker = new UpdateChecker();
				checker.setParent(AboutDialog.this);
				Thread thread = new Thread(checker);
				thread.start();
			}
		});
		buttonPanel.add(closeButton);
		buttonPanel.add(checkUpdatesButton);

		add(editorPane, BorderLayout.CENTER);
		add(buttonPanel, BorderLayout.SOUTH);

		pack();
	}
}
